using System;
using System.Collections.Generic;
using System.Text;
using NLog;
using Barrage.Global;

namespace Barrage.Elements
{
    /// <summary>
    /// Element that holds a list of children elements, each one having this Node instance as their parent.
    /// Also, their motions are relative to this element.
    /// </summary>
    public class Node:Element
    {
        static Logger logger = NLog.LogManager.GetLogger("Barrage.Node");
        List<Element> elements;

        /// <summary>
        /// Constructor. Inits an empty children list
        /// </summary>
        public Node()
        {
            elements = new List<Element>();
        }


        /// <summary>
        /// Adds a children element to the node
        /// </summary>
        /// <param name="e">Element to add</param>
        public void addElement(Element e)
        {
            elements.Add(e);
        }

        /// <summary>
        /// \internal Renders each node children
        /// </summary>
        protected override void DoRender()
        {
            foreach (Element e in elements)
                e.Render();
            logger.Debug("Node Rendered");
        }
        /// <summary>
        /// \internal Updates matrices and each node children
        /// </summary>
        protected override void DoUpdate()
       {
            long startTime = Timer.DefaultTimer.CurrentTotalTime;

            base.DoUpdate();
            foreach (Element e in elements)
            {
                e.Motion.Parent = this.motion;
                e.Update();
            }
            NLog.LogManager.GetLogger(ToString()).Debug(ToString() + " Updated in " + (Timer.DefaultTimer.CurrentTotalTime - startTime) + " ms");            
        }
    }
}
